#include "changewindow.h"

#include <QSqlDatabase>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSqlQuery>
#include <QSqlError>
#include "qdebug.h"

ChangeWindow::ChangeWindow(QWidget *parent) : QWidget(parent)
{
    ISBN       = new QLabel("ISBN");
    name       = new QLabel("书名");
    storage    = new QLabel("库存");
    price      = new QLabel("价格");
    input_ISBN = new QLineEdit();
    input_name = new QLineEdit();
    input_storage= new QLineEdit();
    input_price= new QLineEdit();
    search     = new QPushButton("添加");
    shut       = new QPushButton("关闭");
    initLayout();
    connect(search,&QPushButton::clicked,this,&ChangeWindow::addItem);
    connect(shut,&QPushButton::clicked,this,&ChangeWindow::close);
}
void ChangeWindow::initLayout()
{
    QGridLayout* g = new QGridLayout();
    QHBoxLayout* h = new QHBoxLayout();
    QVBoxLayout* v = new QVBoxLayout();
    QWidget* w1    = new QWidget();
    QWidget* w2    = new QWidget();
    g->addWidget(ISBN,0,0,1,1);g->addWidget(input_ISBN,0,1,1,1);
    g->addWidget(name,1,0,1,1);g->addWidget(input_name,1,1,1,1);
    g->addWidget(storage,2,0,1,1);g->addWidget(input_storage,2,1,1,1);
    g->addWidget(price,3,0,1,1);g->addWidget(input_price,3,1,1,1);
    w1->setLayout(g);
    h->addWidget(search);h->addWidget(shut);
    w2->setLayout(h);
    v->addWidget(w1);v->addWidget(w2);
    this->setLayout(v);
}
void ChangeWindow::addItem()
{
    QString checkISBN=input_ISBN->text();
    QString checkname=input_name->text();
    QString checkstorage=input_storage->text();
    QString checkprice=input_price->text();
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("db1");
    db.setUserName("root");
    db.setPassword("990820");
    if(!db.open())
    {
        qDebug()<<db.lastError().text();
        return;
    }
    QSqlQuery query;
    query.exec("SELECT * FROM BOOK WHERE ISBN = \"" + checkISBN + "\"");
    if(!query.next()) // 结果集为空
    {
        query.exec("INSERT INTO book(ISBN,name,storage,price) VALUES(\""+checkISBN+"\",\""+checkname+"\","+checkstorage+","+checkprice+")");
        msg = new QLabel("查无此书，已添加成功");msg->setAlignment(Qt::AlignCenter);
        delete this->layout();
        QGridLayout* g = new QGridLayout();
        QHBoxLayout* h = new QHBoxLayout();
        QVBoxLayout* v = new QVBoxLayout();
        QWidget* w1    = new QWidget();
        QWidget* w2    = new QWidget();
        g->addWidget(ISBN,0,0,1,1);g->addWidget(input_ISBN,0,1,1,1);
        g->addWidget(name,1,0,1,1);g->addWidget(input_name,1,1,1,1);
        g->addWidget(storage,2,0,1,1);g->addWidget(input_storage,2,1,1,1);
        g->addWidget(price,3,0,1,1);g->addWidget(input_price,3,1,1,1);
        w1->setLayout(g);
        h->addWidget(search);h->addWidget(shut);
        w2->setLayout(h);
        v->addWidget(w1);v->addWidget(w2);v->addWidget(msg);
        this->setLayout(v);
    }
    else // 结果集不为空
    {
        query.exec("DELETE FROM BOOK WHERE ISBN = \"" + checkISBN + "\"");
        query.exec("INSERT INTO book(ISBN,name,storage,price) VALUES(\""+checkISBN+"\",\""+checkname+"\","+checkstorage+","+checkprice+")");
        msg = new QLabel("修改完成");msg->setAlignment(Qt::AlignCenter);
        delete this->layout();
        QGridLayout* g = new QGridLayout();
        QHBoxLayout* h = new QHBoxLayout();
        QVBoxLayout* v = new QVBoxLayout();
        QWidget* w1    = new QWidget();
        QWidget* w2    = new QWidget();
        g->addWidget(ISBN,0,0,1,1);g->addWidget(input_ISBN,0,1,1,1);
        g->addWidget(name,1,0,1,1);g->addWidget(input_name,1,1,1,1);
        g->addWidget(storage,2,0,1,1);g->addWidget(input_storage,2,1,1,1);
        g->addWidget(price,3,0,1,1);g->addWidget(input_price,3,1,1,1);
        w1->setLayout(g);
        h->addWidget(search);h->addWidget(shut);
        w2->setLayout(h);
        v->addWidget(w1);v->addWidget(w2);v->addWidget(msg);
        this->setLayout(v);
    }
    db.close();
}
